package com.oyjd.business.baidu.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.baidu.location.LocServiceMode;
import com.baidu.location.LocationClient;
import com.baidu.location.ReceiveListener;
import com.oyjd.R;
import com.oyjd.framework.log.MyLog;

/**
 * 百度定位使用例子
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class BaiduPosDemo extends Activity {
	private static final String TAG = "BaiduPosActivity";
	private String name = "oyjd_study";
	private LocationClient client;
	public static String COOR_NO = "0";// 不要求返回坐标
	public static String COOR_GC = "gcj02";// 返回国测局经纬度坐标系
	public static String COOR_BAIDU_MKT = "bd09";// 返回百度墨卡托坐标系
	public static String COOR_BAIDU_JW = "bd09ll";// 返回百度经纬度坐标系

	private Thread thread;
	private int p;
	private TextView tv;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_show_txt_demo);

		tv = (TextView) this.findViewById(R.id.tv_content);
		tv.setTextSize(16);
		tv.append("----正在定位----\r\n");
		client = new LocationClient(this.getApplicationContext());
		client.setProdName(name);
		client.setCoorType(COOR_GC);
		client.setAddrType("detail");
		client.setServiceMode(LocServiceMode.Immediat);
		client.openGPS();
		client.addRecerveListener(new ReceiveListener() {

			@Override
			public void onReceive(String arg0) {
				if (arg0 != null) {
					p--;
					tv.append(arg0 + "\r\n");
					if (p == 0) {
						tv.append("----定位结束----");
					}
					MyLog.i(TAG, "异步获取定位结果：" + arg0);
				}
			}
		});

		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (p > 0) {
					client.getLocation();
					try {
						Thread.sleep(4000L);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				client.stop();
				MyLog.i(TAG, "关闭定位SDK");
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		client.start();
		p = 3;
		thread.start();
		MyLog.i(TAG, "启动定位SDK");
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

}
